/// 用户模型类
/// 用于存储和处理用户数据
class User {
  final String id;
  final String username;
  final String email;
  final String avatar;
  final String bio;
  final bool isOnline;
  final DateTime lastSeen;

  User({
    required this.id,
    required this.username,
    required this.email,
    this.avatar = '/uploads/default-avatar.png',
    this.bio = '这个人很懒，什么都没有留下。',
    this.isOnline = false,
    DateTime? lastSeen,
  }) : lastSeen = lastSeen ?? DateTime.now();

  // 从JSON构造函数
  factory User.fromJson(Map<String, dynamic> json) {
    return User(
      id: json['_id'] ?? json['id'] ?? '',
      username: json['username'] ?? '',
      email: json['email'] ?? '',
      avatar: json['avatar'] ?? '/uploads/default-avatar.png',
      bio: json['bio'] ?? '这个人很懒，什么都没有留下。',
      isOnline: json['isOnline'] ?? false,
      lastSeen: json['lastSeen'] != null
          ? DateTime.parse(json['lastSeen'])
          : DateTime.now(),
    );
  }

  // 转换为JSON方法
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'username': username,
      'email': email,
      'avatar': avatar,
      'bio': bio,
      'isOnline': isOnline,
      'lastSeen': lastSeen.toIso8601String(),
    };
  }

  // 复制方法，用于更新用户信息
  User copyWith({
    String? id,
    String? username,
    String? email,
    String? avatar,
    String? bio,
    bool? isOnline,
    DateTime? lastSeen,
  }) {
    return User(
      id: id ?? this.id,
      username: username ?? this.username,
      email: email ?? this.email,
      avatar: avatar ?? this.avatar,
      bio: bio ?? this.bio,
      isOnline: isOnline ?? this.isOnline,
      lastSeen: lastSeen ?? this.lastSeen,
    );
  }

  // 覆盖toString方法，确保返回用户ID而不是整个对象
  @override
  String toString() {
    print('User.toString() 被调用，返回ID: $id');
    // 只返回ID，避免对象序列化问题
    return id;
  }

  // 获取ID的辅助方法，确保安全返回ID字符串
  String safeId() {
    return id;
  }
} 